/* eslint-disable */
<template>
  <!-- 院系管理页面容器 -->
  <div class="departments-container">
    <!-- 页面标题和工具栏 -->
    <div class="page-header">
      <h3>院系管理</h3>
      <!-- 添加院系按钮 -->
      <el-button type="primary" @click="handleAdd">添加院系</el-button>
    </div>

    <!-- 院系列表表格 -->
    <el-table :data="departments" style="width: 100%" v-loading="loading">
      <!-- 院系编号列 -->
      <el-table-column prop="deptNumber" label="学院编号"></el-table-column>
      <!-- 院系代码列 -->
      <el-table-column prop="deptCode" label="学院代码"></el-table-column>
      <!-- 院系名称列 -->
      <el-table-column prop="deptName" label="学院名称"></el-table-column>
      <!-- 院系描述列 -->
      <el-table-column prop="description" label="学院介绍"></el-table-column>
      <!-- 操作列 -->
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.deptNumber)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑院系对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="学院编号" prop="deptNumber">
          <el-input v-model="form.deptNumber" :disabled="dialogTitle === '编辑院系'"></el-input>
        </el-form-item>
        <el-form-item label="学院代码" prop="deptCode">
          <el-input v-model="form.deptCode"></el-input>
        </el-form-item>
        <el-form-item label="学院名称" prop="deptName">
          <el-input v-model="form.deptName"></el-input>
        </el-form-item>
        <el-form-item label="学院介绍" prop="description">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAllDepartments, addDepartment, updateDepartment, deleteDepartment } from '@/api/admin'

export default {
  name: 'AdminDepartments',
  data() {
    return {
      // 加载状态
      loading: false,
      // 院系列表数据
      departments: [],
      // 对话框显示状态
      dialogVisible: false,
      // 对话框标题
      dialogTitle: '',
      // 表单数据
      form: {
        deptNumber: '',
        deptCode: '',
        deptName: '',
        description: ''
      },
      // 表单验证规则
      rules: {
        deptNumber: [
          { required: true, message: '请输入学院编号', trigger: 'blur' }
        ],
        deptCode: [
          { required: true, message: '请输入院系代码', trigger: 'blur' }
        ],
        deptName: [
          { required: true, message: '请输入学院名称', trigger: 'blur' }
        ]
      }
    }
  },
  // 组件创建时获取院系列表
  created() {
    this.fetchDepartments()
  },
  methods: {
    // 格式化日期
    formatDate(date) {
      if (!date) return ''
      return new Date(date).toLocaleString()
    },
    // 获取院系列表
    async fetchDepartments() {
      this.loading = true
      try {
        const response = await getAllDepartments()
        if (response.code === 200) {
          this.departments = response.data
        } else {
          this.$message.error(response.message || '获取院系列表失败')
        }
      } catch (error) {
        console.error('获取院系列表失败:', error)
        this.$message.error('获取院系列表失败')
      } finally {
        this.loading = false
      }
    },
    // 处理添加院系
    handleAdd() {
      this.dialogTitle = '添加院系'
      this.form = {
        deptNumber: '',
        deptCode: '',
        deptName: '',
        description: ''
      }
      this.dialogVisible = true
    },
    // 处理编辑院系
    handleEdit(row) {
      this.dialogTitle = '编辑院系'
      this.form = { ...row }
      this.dialogVisible = true
    },
    // 处理删除院系
    async handleDelete(deptNumber) {
      try {
        await this.$confirm('确认删除该院系吗？', '提示', {
          type: 'warning'
        })
        const response = await deleteDepartment(deptNumber)
        if (response.code === 200) {
          this.$message.success('删除成功')
          this.fetchDepartments()
        } else {
          this.$message.error(response.message || '删除失败')
        }
      } catch (error) {
        if (error !== 'cancel') {
          console.error('删除失败:', error)
          this.$message.error('删除失败')
        }
      }
    },
    // 处理表单提交
    async handleSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          try {
            this.$message.info('正在提交...')
            let response
            if (this.dialogTitle === '添加院系') {
              response = await addDepartment(this.form)
            } else {
              // 确保更新操作使用正确的参数
              response = await updateDepartment(this.form.deptNumber, this.form)
            }
            
            if (response.code === 200) {
              this.$message.success(response.message || (this.dialogTitle === '添加院系' ? '添加成功' : '更新成功'))
              this.dialogVisible = false
              // 刷新院系列表
              this.fetchDepartments()
            } else {
              this.$message.error(response.message || '操作失败')
            }
          } catch (error) {
            console.error('操作失败:', error)
            this.$message.error('操作失败: ' + (error.message || '未知错误'))
          }
        } else {
          this.$message.warning('请完善表单信息')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
/* 院系管理页面容器样式 */
.departments-container {
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
}

/* 页面标题和工具栏样式 */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

/* 页面标题样式 */
.page-header h3 {
  margin: 0;
}
</style> 